package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
import ModelLayer.*;

/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    
    
    public DvdUI()
    {
        dvdCtr = new DVDCtr();
    }
    
    public void start()
    {
        dvdMenu();
    }
    
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice = writeDVDMenu();
            
            if (choice == 1)
            {
                findDVD();
                waitInput();
            }
            else{if(choice == 2){
                   createDVD();
                   waitInput();
                }
                else{
                    if(choice == 3){
                        createCopy();
                        waitInput();
                    }
                    else{
                        if(choice == 4){
                             deleteDVD();
                             waitInput();
                        }
                        else{
                            if(choice == 5){
                                listAllDVDs();
                                waitInput();
                            }
                            else{
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Add DVD title ");
            System.out.println(" (3) Add DVD copy");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    
    private void waitInput()
    {
        System.out.println("Press enter to continue");
        new Scanner(System.in).nextLine();
    }
    
    
    private int inputDVDNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD number: ");
        int no = keyboard.nextInt();
        return no;
    }
        
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    
    private long inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy number: ");
        int no = keyboard.nextInt();
        return no;
    }
    
    // DVD Id number
    private long inputDVDid()
    {
        // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" DVD id:  ");
         long id = keyboard.nextLong();
         return id;
    }
    
    // DVD title name
    private String inputDVDTitle()
    {
        // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" DVD title:  ");
         String title = keyboard.nextLine();
         return title;
    }
    
    // DVD Release Date
    private String inputReleaseDate()
    {
        // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Release Date:  ");
         String releasedate = keyboard.nextLine();
         return releasedate;
    }    

    // DVD artist name
    private String inputArtist()
    {
        // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" DVD artist:  ");
         String artist = keyboard.nextLine();
         return artist;
    }  
    
    // DVD serial number
    private String inputserialnumber()
    {
        // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" DVD serial number:  ");
         String serialnumber = keyboard.nextLine();
         return serialnumber;
    }  
    
    // DVD Purchase Date
    private String inputPurchaseDate()
    {
        // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" DVD purchase date:  ");
         String purchasedate = keyboard.nextLine();
         return purchasedate;
    }
    
    // DVD Purchase Price
    private String inputPurchasePrice()
    {
        // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Purchase Price:  ");
         String purchaseprice = keyboard.nextLine();
         return purchaseprice;
    }
    
    private String inputPublicationDate()
    {
        // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write publication date:  ");
         String pubDate = keyboard.nextLine();
         return pubDate;
    }     
    
    private String inputSearch()
    {
        // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Search for:  ");
         String search = keyboard.nextLine();
         return search;
    }

    private void createDVD()
    { 
        String title = inputTitle();
        long id = inputDVDid();
        String artist = inputArtist();
        String pubDate = inputPublicationDate();
        
        if (title != null && id > 0 && artist != null && pubDate != null)
        {
            dvdCtr.addDVD(title, id, artist, pubDate);
        }
        else
        {
            System.out.println("You didnt use the correct input parameters!");
        }    
    }

    private void findDVD()
    {     
       String search = inputDVDTitle();
       
       if (DVDContainer.getInstance().findDVD(search) != false)
       {    
           DVD foundDVD = dvdCtr.getDVD(search);
           System.out.println("Title: " + foundDVD.getTitle());
           System.out.println("ID: " + foundDVD.getId());
           System.out.println("Artist: " + foundDVD.getArtist());
           System.out.println("Publication Date: " + foundDVD.getPublicationDate());
       }      
       else
       {
            System.out.println("DVD " + search + " is not listed!"); 
       }
       
       
    }   
    
    private void deleteDVD()
    {   
        String search = inputDVDTitle();
        
        if (DVDContainer.getInstance().findDVD(search) != false)
        {
            dvdCtr.deleteDVD(search);
        }
    }
    
    // Create copy of specific DVD
    private void createCopy()
    {   
        String search = inputSearch();
        
        if (DVDContainer.getInstance().findDVD(search) != false)
        {
            String serialNumber = inputserialnumber();
            String purchaseDate = inputPurchaseDate();
            String purchasePrice = inputPurchasePrice();
                        
            if (serialNumber != null && purchaseDate != null && purchasePrice != null)
            {
                dvdCtr.createCopy(search, serialNumber, purchaseDate, purchasePrice);
            }
        }            
    }
    
   
    private void listAllDVDs()
    {
        dvdCtr.listAllDVDs();
    }
}